package spark.core.java;

import scala.math.Ordered;

import java.io.Serializable;

/**
 * 自定义的二次排序
 */
public class SecondarySortKey implements Ordered<SecondarySortKey>, Serializable {


    public SecondarySortKey(int first, int second) {
        this.first = first;
        this.second = second;
    }

    //首先要在自定义key里面，定义需要进行排序的列
    private int first;
    private int second;

    public int compare(SecondarySortKey that) {
        if(this.first - that.getFirst()!=0){
            return  this.first - that.getFirst();
        }else {
            return this.second - that.getSecond();
        }

    }

    public boolean $less(SecondarySortKey that) {
        if(this.first < that.getFirst()){
            return true;
        }else if(this.first==that.getFirst() && this.second > that.getSecond()){
            return true;
        }
        return false;
    }

    public boolean $greater(SecondarySortKey that) {
       if(this.first> that.getFirst()){
           return true;
       }else if(this.first==that.getFirst()&& this.second > that.getSecond()){
           return true;
       }
        return false;
    }

    public boolean $less$eq(SecondarySortKey that) {
       if(this.$less(that)){
           return true;
       }else if(this.first == that.getFirst() && this.second == that.getSecond()){
           return true;
       }
        return false;
    }

    public boolean $greater$eq(SecondarySortKey that) {
        if(this.$greater(that)){
            return true;
        }else if(this.first == that.getFirst()&& this.second == that.getSecond()){
            return true;
        }
        return false;
    }

    public int compareTo(SecondarySortKey that) {
        if(this.first - that.getFirst()!=0){
            return  this.first - that.getFirst();
        }else {
            return this.second - that.getSecond();
        }
    }

    //为进行排序的多个列，提供getter setter方法。以及eques和hashcode方法

    @Override
    public int hashCode() {
        return super.hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        return super.equals(obj);
    }

    public int getFirst() {
        return first;
    }

    public void setFirst(int first) {
        this.first = first;
    }

    public int getSecond() {
        return second;
    }

    public void setSecond(int second) {
        this.second = second;
    }
}
